﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WI.CandidateManager.Business.Objects;
using WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Controllers
{
    public class AttachmentController : AutorizeBaseController
    {
        private readonly AttachmentBO attachmentBO = new AttachmentBO();

        /// <summary>
        /// Shows the attached files of the specified entity.
        /// </summary>
        /// <param name="entityID">The entity ID.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public ActionResult List(long entityID, string returnUrl)
        {
            try
            {
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.EntityId = entityID;
                return View(attachmentBO.GetAttachmentsListByEntityId(entityID));
            }
            catch
            {
                return RedirectToAction("HttpError500", "Error");
            }
        }

        /// <summary>
        /// Uploads file to the DB.
        /// </summary>
        /// <param name="entityId">The entity id.</param>
        /// <param name="fileUpload">The file upload.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <param name="description">The description.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Upload(long entityId, HttpPostedFileBase fileUpload, string returnUrl, string description)
        {
            try
            {
                if (fileUpload != null)
                {
                    Attachment newAttachment = new Attachment();
                    newAttachment.FileName = Path.GetFileName(fileUpload.FileName);
                    newAttachment.BinaryData = new byte[fileUpload.ContentLength];
                    fileUpload.InputStream.Read(newAttachment.BinaryData, 0, fileUpload.ContentLength);

                    newAttachment.ContentType = fileUpload.ContentType;
                    newAttachment.Description = description;
                    newAttachment.EntityBase = attachmentBO.GetEntityById(entityId);
                    attachmentBO.SaveAttachment(newAttachment);
                }
                return RedirectToAction("List", new { area = "", entityId, returnUrl });
            }
            catch
            {
                return RedirectToAction("HttpError500", "Error");
            }
        }

        /// <summary>
        /// Downloads file.
        /// </summary>
        /// <param name="attachmentId">The attachment id.</param>
        /// <returns></returns>
        public ActionResult FileDownload(long attachmentId)
        {
            try
            {
                Attachment attachment = attachmentBO.GetAttachmentById(attachmentId);
                byte[] data = attachment.BinaryData;
                string contentType = attachment.ContentType;
                string fileDownloadName = attachment.FileName;
                return File(data, contentType, fileDownloadName);
            }
            catch
            {
                return RedirectToAction("HttpError500", "Error");
            }
        }

        /// <summary>
        /// Deletes the specified attachment from DB, by id.
        /// </summary>
        /// <param name="attachmentId">The attachment id.</param>
        /// <param name="entityId">The entity id.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public ActionResult Delete(long attachmentId, long entityId, string returnUrl)
        {
            try
            {
                attachmentBO.DeleteAttachment(attachmentId);
                return RedirectToAction("List", new { area = "", entityId, returnUrl });
            }
            catch
            {
                return RedirectToAction("HttpError500", "Error");
            }
        }

        /// <summary>
        /// Return to the page from which passed to "Attachments"
        /// <summary>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public ActionResult GoBack(string returnUrl)
        {
            try
            {
                return Redirect(returnUrl);
            }
            catch
            {
                return RedirectToAction("HttpError500", "Error");
            }
        }
    }
}

